package test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RND {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//testBinomyal();
		svgBNImage(500,500);
	}
	
	private static void svgBNImage(int ii, int jj) {
		StringBuffer img= new StringBuffer();
		for(int i=10;i<ii+10;i++){
			for(int j=10;j<jj+10;j++){
				if(Math.random()>0.5){
					img.append("\n\t<circle cx=\'"+i*2+"\' cy=\'"+j*2+"\' r=\'1' stroke-width='0'/>");
				}
			}
		}
		String svn="<?xml version='1.0'?>" +
				"<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'><svg width='"+ii+"' height='"+jj+"' version='1' xmlns='http://www.w3.org/2000/svg'><g>\n"+
			img+"\n</g></svg>";

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss.SSS");
		
		writeFile("rnd"+sdf.format(new Date())+".svg",svn);

		
	}

	private static void testBinomyal() {
		int classes=20;
		int count=1000000;
		int times = 20;
		int[] values = new int[classes];
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd   HH:mm:ss.SSS");
		for(int j=0;j<times;j++)
		{
			System.out.println(j+") --> Start: "+sdf.format(new Date()));
			for(int i=0;i<count;i++){
				double rnd = (Math.random()*classes+0.5);
				int index = (int) (rnd%classes);
				//System.out.println("rnd:"+rnd+" index:"+index);
				values[index]=values[index]+1;
			}
			
		}
		StringBuffer label = new StringBuffer();
		StringBuffer value = new StringBuffer();
		StringBuffer freq = new StringBuffer();
		double n = ((double)count)*((double)times);
		
		for(int i=0;i<classes;i++){

			double singleFreq=((double)values[i])/n;
			label.append(i+",");
			value.append(values[i]+",");
			freq.append(singleFreq+",");
		}
		String text=label+"\n"+value+"\n"+freq;
		text=text+"\n"+"n:"+n;
		sdf = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss.SSS");
		
		writeFile("rnd"+sdf.format(new Date())+".cvs",text);
		
	}

	public static long randomLong() {
		long x=System.nanoTime();
		  x ^= (x << 21);
		  x ^= (x >>> 35);
		  x ^= (x << 4);
		  return x;
		}
	
	public static void writeFile(String path,String txt) {
	    try {
	      File file = new File(path);
	      FileWriter fw = new FileWriter(file);
	      BufferedWriter bw = new BufferedWriter(fw);
	      bw.write(txt);
	      bw.flush();
	      bw.close();
	      System.out.println("Il file " + path + " e' stato scritto");
	    }
	    catch(IOException e) {
	      e.printStackTrace();
	    }
	  }
}
